﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ABulletF : ABullet
{
    //速度测算公式： 每帧像素(1.5)*帧率(60)/每单位像素(16)=5.625

    //8帧半圈，帧率60->60/(8*2)=3.75圈/秒=1350角度/秒
    public float anglePreSecond = 1350;
    public float radius = 14f / 16f;
    public int bulletCount = 1;

    private List<GameObject> bulletSet = new List<GameObject>();

    private GameObject b;
    private float rotaDirection = 1;
    private void Awake()
    {
        gameObject.SetActive(false);
        rig = GetComponent<Rigidbody2D>();
        b = transform.Find("b").gameObject;
        Vector3 parentPosition = transform.position;
        Vector3 originPosition = Vector3.right * 1;
        float angle = 0;
        if (bulletCount > 0)
        {
            angle = 360f / (bulletCount);
        }
        for (int i = 0; i < bulletCount; i++)
        {
            GameObject bullet = Instantiate(b, transform);
            bulletSet.Add(bullet);
            ABulletFProperty bulletFProperty = bullet.GetComponent<ABulletFProperty>();
            bulletFProperty.hitDistance = hitDistance;
            bulletFProperty.boom = boom;
            bulletFProperty.damage = damage;
            bulletFProperty.SetSource(source);
            Quaternion r = Quaternion.AngleAxis(angle * i, Vector3.forward);
            bullet.transform.position = parentPosition + r * originPosition;
            bullet.SetActive(true);
        }
    }
    void Start()
    {
        GetComponent<SpriteRenderer>().sprite = null;
    }

    // Update is called once per frame
    void Update()
    {
        CommonFly();
    }
    private void FixedUpdate()
    {
        if(targetPosition.x!=float.PositiveInfinity){
            for(int i=0;i<transform.childCount;i++){
                Transform c=transform.GetChild(i);
                if(c.gameObject.activeInHierarchy && c.position.y>=limitRect.max.y){
                    BoomChild(c);
                }
            }
        }


        transform.Rotate(new Vector3(0, 0, rotaDirection * anglePreSecond * Time.deltaTime));
        Vector2 currentPosition = transform.position;
        bool hasActiveBullet = false;
        foreach (GameObject bullet in bulletSet)
        {
            if (bullet.activeInHierarchy)
            {
                hasActiveBullet = true;
                break;
            }
        }
        if (!hasActiveBullet)
        {
            Disappear();
        }
    }

    private void OnDestroy()
    {
        this.CommonOnDestroy();
    }

    public override void Launch(Vector2 direction, Vector2 targetPosition, float speed = 0f)
    {
        base.Launch(direction, targetPosition);
        if (direction.x >= 0)
        {
            rotaDirection = -1f;
        }
        else
        {
            rotaDirection = 1f;
        }
    }

    protected override void Hit(GameObject target)
    {
        throw new System.NotImplementedException();
    }

    protected void BoomChild(Transform c){
        CreateBoom(c.position);
        c.gameObject.SetActive(false);
    }
    protected override void OutOfLimitRange()
    {
        for(int i=0;i<transform.childCount;i++){
            Transform c=transform.GetChild(i);
            if(c.gameObject.activeInHierarchy){
                BoomChild(c);
            }
        }
        Disappear();
    }    
}
